#include "TimerOne.h"
/* first include Arduino.h, the IDE includes it after irmp*.h ... */
#include "Arduino.h"
/* ... and then chokes on uintX_t ... */


extern "C" {
#include "irmpconfig.h"
#include "irmp.h"
#include "irsndconfig.h"
#include "irsnd.h"
}


#define SER_BAUD 115200


/* F_INTERRUPTS is the interrupt frequency defined in irmpconfig.h */
#define US (1000000 / F_INTERRUPTS)


void setup()
{

  Serial.begin(SER_BAUD);
  /* greeting string and debugging ouput */
  Serial.println("Start sending...");
  Serial.print("Interrupt each ");
  Serial.print(US);
  Serial.println(" µs");
  /* configure the input pin. IRMP_BIT is defined in irmpconfig.h,
     default is digital pin 2 */
  //pinMode(IRMP_BIT, INPUT);
  pinMode(IRSND_BIT, OUTPUT);
  //digitalWrite(IRSND_BIT, HIGH);
  delay(20); /* make sure the greeting string is out before starting */
  
  // init IRSND
  //irsnd_init();
  
  //Timer1.initialize(100000);
  // set the interrupt callback function
  //Timer1.attachInterrupt(timerinterrupt);


}


void loop()
{
  
  //IRMP_DATA irmp_data;
  send_close();
  delay(3000);
  /*
  irmpd.protocol = 2;
  irmpd.address = 0x3F40;
  irmpd.command = 0xd;
  irmpd.flags = 0;
  irsnd_send_data(&irmpd, TRUE);
  delay(3000);
  
  
  if (irmp_get_data (&irmp_data))
  {
    Serial.print(irmp_data.protocol);
    Serial.print(":");
    Serial.print(irmp_data.address, HEX);
    Serial.print(":");
    Serial.print(irmp_data.command, HEX);
    Serial.print(":");
    Serial.print(irmp_data.flags, HEX);
    Serial.println("");
    // Relay
    //irsnd_send_data(&irmp_data, TRUE);
    //Serial.println("Data relayed!");
  }*/
  
}

/* helper function: attachInterrupt wants void(), but irmp_ISR is uint8_t() */

void timerinterrupt()
{
  uint8_t ir_detected;
  //uint8_t irsnd_is_on;

  //if(!irsnd_ISR(&irsnd_is_on)) {
  irmp_ISR(&ir_detected);
  Serial.println(ir_detected);
  //}
  /*
  if(count % 30000 == 0) {
    IRMP_DATA irmpd;
    irmpd.protocol = 2;
    irmpd.address = 0x3F40;
    irmpd.command = 0xd;
    irmpd.flags = 0;
    irsnd_send_data(&irmpd, TRUE);
    count = 0; 
    send_close();
  }
  count++;
  */
}


/** manual sending **/

void send_close() {
    digitalWrite(3, LOW);
    // 38kHz pulse for 9000µs => ~ 342 * 26.32µs 
    for(int i=0;i<342;i++) {
      toggle_led();
      delayMicroseconds(26);
    }
    digitalWrite(3, LOW);
    delayMicroseconds(4500); 
    // start bit

    send_bit0();
    send_bit0();
    send_bit0();
    send_bit0();
    send_bit0();
    send_bit0();
    send_bit1();
    send_bit0();
    send_bit1();
    send_bit1();
    send_bit1();
    send_bit1();
    send_bit1();
    send_bit1();
    send_bit0();
    send_bit0();
    // address
    
    send_bit1();
    send_bit0();
    send_bit1();
    send_bit1();
    send_bit0();
    send_bit0();
    send_bit0();
    send_bit0();
    // command
    
    send_bit0();
    send_bit1();
    send_bit0();
    send_bit0();
    send_bit1();
    send_bit1();
    send_bit1();
    send_bit1();
    // inv command
    
    send_bit0();
    // stop bit
}

inline void send_bit0() {
    // 38kHz pulse for 560µs => ~ 21.28 * 26.32µs 
    for(int i=0;i<21;i++) {
      toggle_led();
      delayMicroseconds(26);
    }
    toggle_led();
    delayMicroseconds(560);
}
inline void send_bit1() {
    // 38kHz pulse for 560µs => ~ 21.28 * 26.32µs 
    for(int i=0;i<21;i++) {
      toggle_led();
      delayMicroseconds(26);
    }
    toggle_led();
    delayMicroseconds(1690);
}

inline void toggle_led() {
    digitalWrite(3, 1-digitalRead(3));
}
